/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-09-23
 * Time: 18:09
 */
public class count_AndSay {
    /**
     * @param n 给定一个整数 n
     * @return 外观数列
     */
    public static String countAndSay(int n) {
        if (n == 1) {
            return "1";
        }

        if (n == 2) {
            return "11";
        }

        if (n == 3) {
            return "21";
        }

        StringBuffer stringBuffer = new StringBuffer();

        stringBuffer.append("21");
        int i = 3;
        while (i < n) {

            StringBuffer tmp = new StringBuffer();

            int left = 0, right = 0;
            while (right < stringBuffer.length()) {
                if (stringBuffer.charAt(right) != stringBuffer.charAt(left)) {
                    tmp.append(right - left);
                    tmp.append(stringBuffer.charAt(left));
                    left = right;
                }
                right++;
            }

            tmp.append(right - left);
            tmp.append(stringBuffer.charAt(left));

            stringBuffer = tmp;
            i++;
        }

        return stringBuffer.toString();
    }

    public static void main(String[] args) {
        System.out.println(countAndSay(4));
    }
}
